<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="login-title">🔐</span>
      </div>
      <div class="login-form">
        <el-form :model="form" ref="loginForm">
          <el-form-item prop="userName">
            <el-input type="text" v-model="form.userName" auto-complete="off" placeholder="请输入用户名">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input type="text" v-model="form.passWord" auto-complete="off" placeholder="请输入密码">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-key"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width:100%;" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/login/index.js'
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      form: {
        userName: '',
        passWord: ''
      },
      loginRules: {
        userName: [
          { required: true, message: '请输入账户', trigger: 'blur' },
        ],
        passWord: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.$socket.on('message', (data) => {
      console.log(data);
    })
  },
  methods: {
    handleLogin() {
      const id = sessionStorage.getItem('socketId')
      login({ socketId: id, username: this.form.userName, userpassword: this.form.passWord }).then(res => {
        this.$message({
          message: `${res.data.message}`,
          type: `${res.data.dotoState !== '1' ? 'warning' : 'success'}`
        });
        if (res.data.dotoState === '1') {
          sessionStorage.setItem("userid", res.data.userid)
          this.$router.replace({
              path: '/index',

            })
        } else if (res.data.dotoState === '2') {
          // this.$confirm('当前登录用户已在线, 是否继续登录?', '提示', {
          //   confirmButtonText: '确定',
          //   cancelButtonText: '取消',
          //   type: 'warning'
          // }).then(() => {
          this.$socket.emit('userLogin', { socketId: id, username: this.form.userName, userpassword: this.form.passWord })
          this.$socket.on(`timSend`, (data) => {
            sessionStorage.setItem("userid", data.userid)
            this.$router.replace({
              path: '/index',

            })
          });
          // }).catch(() => {
          //   this.$message({
          //     type: 'info',
          //     message: '已取消'
          //   });
          // });
        }
      }).catch(e => { console.log(e); })
    }
  }
}
</script>

<style scoped>
.login-page {
  background-image: linear-gradient(180deg, #2af598 0%, #009efd 100%);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-title {
  font-size: 20px;
}

.box-card {
  width: 375px;
}
</style>

